<template>
  <div>
    <div class="title">
      <img src="../../../../../../assets/app/icon/picture.png" alt="" />
      <h5>图片导航</h5>
    </div>
    <operation-title
      title="图片导航"
      :icon="TitleIcon"
    />
    <div class="divider"></div>
    <div class="func">
      <p class="attr">样式</p>
      <ul class="common-ul">
        <li
          @click="config.pageConfig.style = '1'"
          :class="config.pageConfig.style == '1' ? 'active' : ''"
        >
          默认
        </li>
        <li
          @click="config.pageConfig.style = '2'"
          :class="config.pageConfig.style == '2' ? 'active' : ''"
        >
          投影
        </li>
        <li
          @click="config.pageConfig.style = '3'"
          :class="config.pageConfig.style == '3' ? 'active' : ''"
        >
          描边
        </li>
      </ul>
      <div class="adjust-item" v-if="config.pageConfig.style == '3'">
        <el-color-picker v-model="config.pageConfig.borderColor" />
        <span>{{ config.pageConfig.borderColor }}</span>
      </div>
      <div class="adjust-item" v-if="config.pageConfig.style == '3'">
        <el-slider
          v-model="config.pageConfig.borderWidth"
          :min="0"
          :max="5"
          show-input
          size="small"
        />px
      </div>
    </div>
    <div class="divider"></div>
    <div class="func">
      <p class="attr">内容设置</p>
      <h4>建议图片宽度226px，高度226px。要求图片高度必须完全一致</h4>
      <ul v-if="config.pageConfig.imgList.length > 0" class="add-item">
        <li v-for="(item, index) in config.pageConfig.imgList" :key="index">
          <div class="add-item-input">
            <p>图片</p>
            <div class="upload-img" v-if="item.imgUrl">
              <el-image
                style="width: 100%; height: 100%"
                :src="item.imgUrl"
                fit="contain"
              ></el-image>
              <div class="upload-img_cover">
                <Upload
                  :model-value="fileList"
                  file-path="act/"
                  :limit="999"
                  :show-file-list="false"
                  @success="uploadSuccess"
                >
                  <el-button @click="currentIndex = index" size="small"
                    >替换</el-button
                  >
                </Upload>
              </div>
            </div>
            <Upload
              v-else
              v-model="item.imgUrl"
              file-path="act/"
              list-type="picture-card"
            >
              <el-icon>
                <Plus />
              </el-icon>
            </Upload>
            <!-- <img :src="item.imgUrl" alt="" v-if="item.imgUrl" />
            <img src="@/assets/app/bar/default_picture.png" alt="" /> -->
          </div>
          <div class="add-item-input">
            <p>主标题</p>
            <el-input
              v-model="item.title"
              maxlength="9"
              :show-word-limit="true"
            />
          </div>
          <div class="add-item-input">
            <p>颜色</p>
            <el-color-picker v-model="item.titleColor" />
            <span>{{ item.titleColor }}</span>
          </div>
          <div class="add-item-input">
            <p>副标题</p>
            <el-input
              v-model="item.subtitle"
              maxlength="10"
              :show-word-limit="true"
            />
          </div>
          <div class="add-item-input">
            <p>颜色</p>
            <el-color-picker v-model="item.subtitleColor" />
            <span>{{ item.subtitleColor }}</span>
          </div>
          <div class="add-item-input">
            <p>链接</p>
            <div class="add-item-link" @click="linkDialog(item, index)">
              <el-icon>
                <Link />
              </el-icon>
              <span v-if="item.url">{{ item.urlName }}</span>
              <span v-else>请选择链接</span>
              <!-- <span v-if="item.name">请选择链接</span>
              <span v-else>请选择链接</span>
              <el-icon v-if="item.name" class="close-btn"><Close /></el-icon> -->
            </div>
          </div>
          <div class="add-item-input">
            <p>背景色</p>
            <el-color-picker v-model="item.color" />
            <span>{{ item.color }}</span>
          </div>
          <div class="move-btns">
            <el-icon
              v-show="index != 0"
              class="move-icon"
              title="上移"
              @click="moveTo('up', index)"
            >
              <Top />
            </el-icon>
            <el-icon
              v-show="index != config.pageConfig.imgList.length - 1"
              style="cursor: pointer"
              title="下移"
              @click="moveTo('down', index)"
            >
              <Bottom />
            </el-icon>
          </div>
          <el-popconfirm
            confirm-button-text="是"
            cancel-button-text="否"
            @confirm="sliceItem(index)"
            :title="
              config.pageConfig.imgList.length > 1
                ? '确定删除吗？'
                : '只有一个不可删除'
            "
          >
            <template #reference>
              <el-icon class="sure-close">
                <Close />
              </el-icon>
            </template>
          </el-popconfirm>
        </li>
        <li
          class="extra-li"
          @click="addItem"
          v-if="config.pageConfig.imgList.length < 3"
        >
          <p>+添加</p>
        </li>
      </ul>
    </div>
    <div class="divider"></div>
    <div class="func">
      <p class="attr">文字大小</p>
      <div class="adjust-item">
        <span>主标题</span>
        <el-slider
          v-model="config.pageConfig.titleSize"
          :min="12"
          :max="16"
          show-input
          size="small"
        />px
      </div>
      <div class="adjust-item">
        <span>副标题</span>
        <el-slider
          v-model="config.pageConfig.subtitleSize"
          :min="12"
          :max="16"
          show-input
          size="small"
        />px
      </div>
    </div>
    <div class="divider"></div>
    <div class="func">
      <p class="attr">选择颜色</p>
      <div class="adjust-item">
        <span>背景</span>
        <el-color-picker v-model="config.pageConfig.color" />
        <span>{{ config.pageConfig.color }}</span>
      </div>
    </div>
    <div class="divider"></div>
    <div class="func">
      <p class="attr">边距</p>
      <div class="adjust-item">
        <span>上边距</span>
        <el-slider
          v-model="config.pageConfig.marginTop"
          :min="0"
          :max="50"
          show-input
          size="small"
        />px
      </div>
      <div class="adjust-item">
        <span>下边距</span>
        <el-slider
          v-model="config.pageConfig.marginBottom"
          :min="0"
          :max="50"
          show-input
          size="small"
        />px
      </div>
      <div class="adjust-item">
        <span>左右边距</span>
        <el-slider
          v-model="config.pageConfig.margin"
          :min="10"
          :max="20"
          show-input
          size="small"
        />px
      </div>
    </div>
    <div class="divider"></div>
    <div class="func">
      <p class="attr">圆角设置</p>
      <div class="adjust-item">
        <span>上圆角</span>
        <el-slider
          :min="0"
          :max="20"
          v-model="config.pageConfig.radiusTop"
          show-input
          size="small"
        />px
      </div>
      <div class="adjust-item">
        <span>下圆角</span>
        <el-slider
          :min="0"
          :max="20"
          v-model="config.pageConfig.radiusBottom"
          show-input
          size="small"
        />px
      </div>
    </div>
    <div class="divider"></div>
    <href-selector
      v-model:show="linkFormVisible"
      :model-value="linkObject"
      @confirm="(link) => submitUrl(link, config.pageConfig.imgList[linkIndex])"
    />
  </div>
</template>

<script lang="ts">
export default {
  name: "ImgCarouselOperation",
};
</script>
<script lang="ts" setup>
import { reactive, onMounted } from "vue";
import { useHrefSelector } from "@/views/activity/actApp/components/default/compositions/useHrefSelector";
import TitleIcon from "@/assets/app/icon/picture.png";
import OperationTitle
  from "@/views/activity/actApp/components/default/operationComponents/operationTitle/OperationTitle.vue";
const config = defineProps({
  pageConfig: {
    type: Object,
    default: {
      style: "1",
      color: "transparent",
      borderColor: "transparent",
      borderWidth: 1,
      marginTop: 0,
      marginBottom: 0,
      margin: 10,
      radiusTop: 0,
      radiusBottom: 0,
      imgList: [
        {
          title: "主标题",
          subtitle: "副标题",
          titleColor: "#3d404d",
          subtitleColor: "#5e6066",
          color: "transparent",
          imgUrl: "",
          name: "",
          url: "",
          urlType: "",
          urlName: "",
        },
        {
          title: "主标题",
          subtitle: "副标题",
          titleColor: "#3d404d",
          subtitleColor: "#5e6066",
          color: "transparent",
          imgUrl: "",
          name: "",
          url: "",
          urlType: "",
          urlName: "",
        },
        {
          title: "主标题",
          subtitle: "副标题",
          titleColor: "#3d404d",
          subtitleColor: "#5e6066",
          color: "transparent",
          imgUrl: "",
          name: "",
          url: "",
          urlType: "",
          urlName: "",
        },
      ],
    },
  },
});
const sliceItem = (idx: Number) => {
  if (config.pageConfig.imgList.length < 2) {
    return;
  } else {
    config.pageConfig.imgList.splice(idx, 1);
  }
};
const addItem = () => {
  if (config.pageConfig.imgList.length >= 3) {
    return;
  } else {
    config.pageConfig.imgList.push({
      title: "主标题",
      subtitle: "副标题",
      titleColor: "#3d404d",
      subtitleColor: "#5e6066",
      color: "#fff",
      imgUrl: "",
      name: "",
      url: "",
    });
  }
};
let emits = defineEmits<{ (e: "changeStyle", configStyle: Object): void }>();
const currentIndex = ref(999);
const fileList = ref([]);
const uploadSuccess = (value: any) => {
  config.pageConfig.imgList[currentIndex.value].imgUrl = value.url;
};
const moveTo=(type:'up'|'down',index:number)=>{
  const list = [...config.pageConfig.imgList]
  const item = list[index]
  //上移
  if (type == 'up') {
    const preItem = list[index - 1]
    list[index - 1] = item
    list[index] = preItem
  }
  //下移
  if (type == 'down') {
    const nextItem = list[index + 1]
    list[index] = nextItem
    list[index + 1] = item
  }
  config.pageConfig.imgList = [...list]
}

// let linkIndex = 0;
// const linkFormVisible = ref(false);
// const linkObject = ref({
//   linkUrl: '',
//   linkName: '',
//   linkType: '',
//   linkHttpUrl: ''
// });
// const submitUrl = (content: any) => {
//   config.pageConfig.imgList[linkIndex].url = content.url;
//   config.pageConfig.imgList[linkIndex].urlName = content.urlName;
//   config.pageConfig.imgList[linkIndex].urlType = content.urlType;
//   linkFormVisible.value = false
// };
// const onClose = () => {
//   linkFormVisible.value = false
// };
// const linkDialog = (item: any, idx: number) => {
//   linkIndex = idx;
//   if (item.urlType == 'link') {
//     linkObject.value.linkHttpUrl = item.url;
//     linkObject.value.linkUrl = '';
//     linkObject.value.linkName = item.urlName;
//     linkObject.value.linkType = item.urlType;
//   } else {
//     linkObject.value.linkHttpUrl = '';
//     linkObject.value.linkUrl = item.url;
//     linkObject.value.linkName = item.urlName;
//     linkObject.value.linkType = item.urlType;
//   }
//   linkFormVisible.value = true;
// };

const { linkFormVisible, linkIndex, linkObject, submitUrl, linkDialog } =
  useHrefSelector();

watch(config, (val) => {
  emits("changeStyle", config.pageConfig);
});
</script>

<style lang="scss" scoped>
.title {
  padding: 8px 23px;
  overflow: hidden;
  border-bottom: 5px solid #f6f7f9;
  display: flex;
  position: relative;
  height: 50px;
  box-sizing: border-box;

  img {
    position: absolute;
    left: 0;
    top: -5px;
  }

  h5 {
    font-size: 16px;
    font-weight: 700;
    position: absolute;
    left: 70px;
    top: 0;
    line-height: 50px;
  }
}

.func {
  border-bottom: 5px solid #f6f7f9;
  padding: 20px 16px;
  font-size: 16px;

  .attr {
    line-height: 1;
    color: #47565d;
    font-weight: bolder;
    margin-bottom: 20px;
  }

  ul.common-ul {
    list-style: none;
    display: flex;
    align-items: center;
    justify-content: center;

    li {
      flex: 1;
      height: 30px;
      display: flex;
      align-items: center;
      justify-content: center;
      border-radius: 4px 0 0 4px;
      border: 1px solid #ededed;
      border-collapse: collapse;

      &.active {
        border: 1px solid #fb6638;
      }

      img {
        max-width: 100% !important;
        max-height: 100% !important;
      }
    }
  }

  h4 {
    position: relative;
    padding: 8px 16px 8px 16px;
    border-radius: 6px;
    color: red;
    font-size: 16px;
    font-weight: 600;
    line-height: 22px;
    margin-bottom: 10px;
    border: 1px solid #d8e8fc;
    background-color: #e5f0fe;
  }

  div.adjust-item {
    display: flex;
    align-items: center;
    margin-top: 3px;
    margin-bottom: 10px;

    span {
      width: 64px;
      text-align: right;
      margin-right: 15px;
      color: #666;
    }

    :deep(.el-slider) {
      width: 240px;
      margin-right: 3px;
      margin-left: 15px;
    }

    :deep(.el-slider__runway.show-input) {
      margin-right: 15px;
    }

    :deep(.el-input-number) {
      width: 100px;
      background: #f4f3f7;
      border: 1px solid #f4f3f7;
    }
  }

  .add-item {
    width: 100%;
    list-style: none;

    li {
      background: #f4f3f7;
      border-radius: 4px;
      padding: 10px 20px 10px 10px;
      color: #999;
      margin-top: 10px;
      margin-bottom: 20px;
      position: relative;

      .add-item-input {
        display: flex;
        align-items: center;
        margin-bottom: 20px;

        > p {
          width: 58px;
          margin-right: 8px;
        }

        .upload-img {
          width: 50px;
          height: 50px;
          margin-right: 10px;
          position: relative;

          :deep(.el-image) {
            width: 50px;
            height: 50px;
          }

          &:hover {
            .upload-img_cover {
              opacity: 1;
            }
          }

          .upload-img_cover {
            opacity: 0;
            position: absolute;
            transition: all 0.3s ease;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.7);
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: space-evenly;
          }
        }

        :deep(.el-upload-list) {
          width: 50px;
          height: 50px;
          margin-right: 10px;
        }

        :deep(.el-upload--picture-card) {
          width: 50px;
          height: 50px;
        }

        // img {
        //   width: 40px;
        //   height: 40px;
        // }
        :deep(.el-color-picker) {
          margin-right: 10px;
        }
      }

      .add-item-link {
        width: calc(100% - 80px);
        display: flex;
        align-items: center;
        background: #e8effc;
        border: 0;
        padding-left: 16px;
        cursor: pointer;
        position: relative;

        :deep(.el-icon) {
          margin-right: 10px;
        }

        :deep(.el-input) {
          font-size: 12px;
          height: 30px;
          width: calc(100% - 30px);
        }

        .close-btn {
          position: absolute;
          right: 5px;
        }
      }

      .sure-close {
        position: absolute;
        right: 5px;
        top: 5px;
        cursor: pointer;
      }
    }

    .extra-li {
      text-align: center;
    }
  }
}
.move-btns {
  position: absolute;
  top: 5px;
  right: 24px;
  .move-icon {
    margin-right: 4px;
    cursor: pointer;
  }
}
</style>
